part of '../repository.dart';

const _kLexicalItemMetaProgressKey = 'progress';
const _kLexicalItemMetaExcludedKey = 'excluded';

class _LexicalItemMetaDto extends Equatable {
  const _LexicalItemMetaDto._internal({
    required this.progressDto,
    required this.excluded,
  });

  final _LexicalItemProgressDto? progressDto;
  final bool excluded;

  @override
  List<Object?> get props {
    return [
      progressDto,
      excluded,
    ];
  }

  factory _LexicalItemMetaDto.fromJson(Map<String, dynamic> json) {
    _LexicalItemProgressDto? lexicalItemProgressDto;

    final lexicalItemProgressMap = json[_kLexicalItemMetaProgressKey] as Map?;

    if (lexicalItemProgressMap != null) {
      final lexicalItemProgressJson =
          Map.castFrom<dynamic, dynamic, String, dynamic>(lexicalItemProgressMap);

      lexicalItemProgressDto = _LexicalItemProgressFactoryDto().fromJson(lexicalItemProgressJson);
    }

    return _LexicalItemMetaDto._internal(
      progressDto: lexicalItemProgressDto,
      excluded: json[_kLexicalItemMetaExcludedKey],
    );
  }

  factory _LexicalItemMetaDto.fromLexicalItemMeta(BackupLexicalItemMeta lexicalItemMeta) {
    _LexicalItemProgressDto? lexicalItemProgressDto;

    if (lexicalItemMeta.progress != null) {
      lexicalItemProgressDto =
          _LexicalItemProgressFactoryDto().fromEntity(lexicalItemMeta.progress!);
    }

    return _LexicalItemMetaDto._internal(
      progressDto: lexicalItemProgressDto,
      excluded: lexicalItemMeta.excluded,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      _kLexicalItemMetaProgressKey: progressDto?.toJson(),
      _kLexicalItemMetaExcludedKey: excluded,
    };
  }

  BackupLexicalItemMeta toEntity() {
    return BackupLexicalItemMeta(
      progress: progressDto?.toEntity(),
      excluded: excluded,
    );
  }
}
